package com.hh.erp.controller;


import com.hh.erp.entity.SysRole;
import com.hh.erp.entity.SysUser;
import com.hh.erp.entity.request.SysUserRequest;
import com.hh.erp.entity.vo.SysUserExcel;
import com.hh.erp.service.SysPostService;
import com.hh.erp.service.SysRoleService;
import com.hh.erp.service.SysUserService;

import com.hh.erp.util.JsonUtil;
import com.hh.erp.util.excel.ExcelUtil;
import com.hh.erp.util.http.AjaxResult;
import com.hh.erp.util.http.DiscoveryException;
import com.hh.erp.util.page.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private SysUserService userService;
    @Autowired
    private SysRoleService roleService;

    @Autowired
    private SysPostService postService;

    @GetMapping("/list")
    public AjaxResult list(SysUserRequest request) {
        PageInfo page = userService.selectPageUserList(request);
        return AjaxResult.success(page);
    }

    /**
     * 有ID时把用户信息返出去，没ID就是查询所有角色和岗位的数据
     * @param Id
     */
    @GetMapping(value = { "/", "/{userId}" })
    public AjaxResult getDictDataInfo(@PathVariable(value ="userId", required = false) Long Id) {

        AjaxResult result = AjaxResult.success();
        if(Id != null) {
            SysUser user = userService.findUserById(Id);
            if (user == null) {
                throw new DiscoveryException("查询的用户不存在！");
            }
            List<SysRole> userRoles = roleService.selectRoleByUserId(Id);
            result.put("data", user);
            result.put("postIds", postService.selectPostListByUserId(Id));
            result.put("roleIds", userRoles.stream().map(SysRole::getRoleId).collect(Collectors.toList()));
        }

        result.put("roles", roleService.selectRoleAll());
        result.put("posts", postService.selectPostAll());
        return result;
    }

    @PutMapping("")
    public AjaxResult edit(@RequestBody SysUserRequest request) {
        if(userService.edit(request))
            return AjaxResult.success();
        return AjaxResult.error();
    }


    @PostMapping("")
    public AjaxResult add(@RequestBody SysUserRequest request) {
        if(userService.add(request))
            return AjaxResult.success();
        return AjaxResult.error();
    }

    /**
     * 修改用户状态
     * @param request
     * @return
     */
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody SysUserRequest request) {
        if(userService.changeStatus(request))
            return AjaxResult.success();
        return AjaxResult.error();
    }

    /**
     *  重置用户密码（后续要限制用户权限）
     * @param request
     * @return
     */
    @PutMapping("/resetPwd")
    public AjaxResult resetPwd(@RequestBody SysUserRequest request) {
        if(userService.resetPwd(request))
            return AjaxResult.success();
        return AjaxResult.error();
    }


    @DeleteMapping("/{userId}")
    public AjaxResult delete(@PathVariable(value ="userId", required = false) Long Id) {
        if(userService.deleteById(Id))
            return AjaxResult.success();
        return AjaxResult.error();
    }

    /**
     * 用户数据导入
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/importData")
    public AjaxResult importData(@RequestPart("file")MultipartFile file)  {
       List<SysUserExcel> list = ExcelUtil.readExcel(file,SysUserExcel.class);
       System.out.println(JsonUtil.toJson(list));
        return AjaxResult.success(list);
    }

}
